<?php
class Admin_AgentController extends Zend_Controller_Action
{
    public function init ()
    {}
    public function indexAction ()
    {}
    public function registerAction ()
    {}
    public function imodAction ()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $request = $this->getRequest();
        $params = array_diff($request->getParams(), $request->getUserParams());
        foreach ($params as $colName => $value) {
            $value = is_array($value) ? $value : htmlentities(trim($value));
            $params[$colName] = $value;
        }
        $oper = strtolower($params['oper']);
        switch ($oper) {
            case 'add':
                $dobDateobj = new Zend_Date($params['dob'], 'dd-MM-yyyy');
                $params['dob'] = $dobDateobj->toString('YYYY-MM-dd');
                $agent = new Reale_Model_Users_Agent($params);
                $ppostalcode = $params['ppostalcode'];
                $pstate = $params['pstate'];
                $pdistrict = $params['pdistrict'];
                $pcity = $params['pcity'];
                $padd = $params['padd'];
                if (isset($params['husband_name'])) {
                    $agent->setHusband_name($params['husband_name']);
                }
                $paddress = new Reale_Model_Location();
                $paddress->setAddressline($padd)
                    ->setPlace($pcity)
                    ->setDistrict($pdistrict)
                    ->setState($pstate)
                    ->setPostalcode($ppostalcode);
                $agent->setPaddress($paddress);
                if ($params['is_cp_same'] == false) {
                    $caddress = new Reale_Model_Location();
                    $caddress->setAddressline($params['cadd'])
                        ->setPlace($params['ccity'])
                        ->setDistrict($params['cdistrict'])
                        ->setState($params['cstate'])
                        ->setPostalcode($params['cpostalcode']);
                    $agent->setCaddress($caddress);
                }
                $nominee = new Reale_Model_Users_Nominee();
                $naddress = new Reale_Model_Location();
                if ($params['is_np_same'] == false) {
                    $naddress->setAddressline($params['nadd'])
                        ->setPlace($params['ncity'])
                        ->setDistrict($params['ndistrict'])
                        ->setState($params['nstate'])
                        ->setPostalcode($params['npostalcode']);
                    $nominee->setAddress($naddress);
                } else {
                    $nominee->setAddress($paddress);
                }
                $nominee->setNominee_name($params['nominee_name'])
                    ->setRelation($params['nominee_relation'])
                    ->setPan($params['nominee_pan']);
                $agent->setNominee($nominee);
                $sponsor_id = $params['sponsor_id'];
                $rgrnDateobj = new Zend_Date(null, 'dd-MM-yyyy');
                $rgrn_date = $rgrnDateobj->toString('YYYY-MM-dd');
                $agent->setRgrn_date($rgrn_date);
                $sponsor = new Reale_Model_Users_Agent(
                array('agent_reg_id' => $sponsor_id));
                $agent->setSponsor($sponsor);
                if ($agent->save()) {
                    echo $agent->getAgent_name() .
                     ' is now registered as agent, with agent id: ' .
                     $agent->getAgent_reg_id();
                    return;
                }
                break;
            default:
                throw new Zend_Exception('"' . $oper . '" is unknown operation.', 
                Zend_Log::ERR);
                break;
        }
        throw new Zend_Exception(
        'Operation on agent "' . $agent->getUsername() . '" not successfull.', 
        Zend_Log::ERR);
    }
    public function cansponsorAction ()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $request = $this->getRequest();
        $agent_reg_id = $request->getParam('agent_reg_id');
        $agent = new Reale_Model_Users_Agent(
        array('agent_reg_id' => $agent_reg_id));
        $sponsered = $agent->canSponsor();
        if (false === $sponsered) {
            throw new Zend_Exception('Can not sponsor anymore.', Zend_Log::ERR);
        } else {
            echo 'Agents sponsered yet :' . $sponsered;
        }
    }
    public function tempAction ()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $agent = new Reale_Model_Users_Agent(
        array('agent_reg_id' => 'AGNT-A-512'));
        echo '<pre>';
        //$result = $agent->fetchDownLine(null,null);
        //$agentMapper = new Reale_Model_Users_AgentMapper();
        $result = '';
        //print_r($result);
        $this->_helper->logger($result);
    }
    public function pendingdisburseAction ()
    {
        $mapper = new Reale_Model_Users_AgentMapper();
        $pending = $mapper->fetchPendingDisbursements();
        $this->view->assign('pending', $pending);
    }
    public function disburseAction ()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $request = $this->getRequest();
        $params = array_diff($request->getParams(), $request->getUserParams());
        foreach ($params as $colName => $value) {
            $value = is_array($value) ? $value : htmlentities(trim($value));
            $params[$colName] = $value;
        }
        if (count($params)) {
            $agent = new Reale_Model_Users_Agent($params);
            $status = $agent->disburse($params['net_trans']);
            if ($status) {
                return "Sucessfully disbursed for Agent id:" +
                 $agent->getAgent_reg_id();
            }
        } else {
            throw new Exception('Parameters are required to process request', 
            Zend_Log::WARN);
        }
    }
    public function dispatchAction ()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $request = $this->getRequest();
        $params = array_diff($request->getParams(), $request->getUserParams());
        foreach ($params as $colName => $value) {
            $value = is_array($value) ? $value : htmlentities(trim($value));
            $params[$colName] = $value;
        }
        if (count($params)) {
            $earning = new Reale_Model_DbTable_AgentEarnings();
            try {
                $now = new Zend_Db_Expr('NOW()');
                $data = array('is_dispatched'=>'1','dispatch_date'=>$now);
                $where = 'ab_trans = ' . $params['ab_trans'];
                $status = $earning->update($data, $where);
                if ($status) {
                    echo "Sucessfully updated dispatch status. ($status rows).";
                }
            } catch (Exception $e) {
                $this->_helper->logger($e->getMessage());
                throw new Exception('Unable to update dispatch status.', Zend_Log::ERR);
            }
        } else {
            throw new Exception('Parameters are required to process request', 
            Zend_Log::WARN);
        }
    }

    public function pendingdispatchAction ()
    {
        $earnings = new Reale_Model_DbTable_AgentEarnings();
        $pending = $earnings->networkTransactions();
        $this->view->assign('pending', $pending);
    }
    
    
    public function networkviewAction ()
    {
        $data['agent_reg_id'] = $this->getRequest()->getParam('agent','AGNT-A-0');
		$agent = new Reale_Model_Users_Agent($data);
		$downLine = $agent->fetchDownLine(null);
		$this->view->assign('agent',$data['agent_reg_id']);
		$this->view->assign('downline',$downLine);
    }
    
}